package 力扣链表;

import java.util.HashSet;
import java.util.Set;

public class Leedcode141环形链表 {
    public boolean hasCycle1(ListNode head) {
        if (head == null || head.next == null){
            return false;
        }
        //方法1：快慢指针
        ListNode slow = head;
        ListNode fast = head.next;
        while (slow != fast){
            if (fast == null || fast.next == null){
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }


    //方法1：set去重
    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        int count = 0;
        while (head != null){
            if (set.contains(head)){
                return true;
            }
            set.add(head);
            head = head.next;
        }
        return false;
    }
}
